/*
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
 *
 * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
 * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
 *
 * Contributor(s): 
 *  Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
 *  
 * If you didn't download this code from the following link, you should check
 * if you aren't using an obsolete version: http://www.isqlviewer.com
 */
package org.isqlviewer.ui.wizards.service;

import static java.awt.GridBagConstraints.CENTER;
import static java.awt.GridBagConstraints.HORIZONTAL;
import static java.awt.GridBagConstraints.NONE;
import static java.awt.GridBagConstraints.VERTICAL;
import static java.awt.GridBagConstraints.WEST;

import java.awt.Container;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import org.isqlviewer.sql.ConnectionProfile;
import org.isqlviewer.sql.JdbcService;
import org.isqlviewer.swing.SwingUtilities;
import org.isqlviewer.ui.wizards.AbstractWizardStep;
import org.isqlviewer.ui.wizards.WizardContext;
import org.isqlviewer.util.LocalMessages;

public class ServiceProfileOptions extends AbstractWizardStep implements ActionListener {

    private LocalMessages messages = new LocalMessages(ServiceWizard.BUNDLE_NAME);
    private JTextField preferredSchema = new JTextField();
    private JTextField preferredCatalog = new JTextField();
    private JCheckBox secureCredentials = new JCheckBox();
    private JCheckBox serviceDebugging = new JCheckBox();
    private JCheckBox escapeProcessing = new JCheckBox();
    private JCheckBox generateKeys = new JCheckBox();

    public void actionPerformed(ActionEvent event) {

    }

    @Override
    public void activate(WizardContext context) {

        super.activate(context);
        JdbcService service = (JdbcService) context.getAttribute(ServiceWizard.ATTRIBUTE_SERVICE);
        if (service == null) {
            return;
        }

        ConnectionProfile profile = service.getProfile();
        preferredCatalog.setText(profile.getPreferredCatalog());
        preferredSchema.setText(profile.getPreferredSchema());
        secureCredentials.setSelected(profile.isCredentialsSecure());
        serviceDebugging.setSelected(profile.isDebuggingEnabled());
        escapeProcessing.setSelected(profile.isEscapeProcessing());
        generateKeys.setSelected(profile.isResultsetKeys());
    }

    public boolean isFirst() {

        return false;
    }

    public boolean isLast() {

        return false;
    }

    @Override
    public void init(WizardContext context) {

        super.init(context);
        setTitle(messages.getMessage("ServiceProfileOptions.title"));
        setComment(messages.getMessage("ServiceProfileOptions.tip"));
        setImage(SwingUtilities.loadIconResource("options", 22));

        Container uiContainer = new JPanel(new GridBagLayout());
        setView(new JScrollPane(uiContainer));

        String tip = null;
        String title = null;
        JLabel label = null;
        Object constraint = null;

        tip = messages.format("ServiceProfileOptions.preferred-catalog.tip");
        title = messages.format("ServiceProfileOptions.preferred-catalog.title");
        preferredCatalog.setToolTipText(tip);
        label = new JLabel(title);
        label.setToolTipText(tip);
        label.setLabelFor(preferredCatalog);
        constraint = ServiceWizard.constrain(0, 0, 1, 1, 0.0, 0.0, WEST, NONE);
        uiContainer.add(label, constraint);
        constraint = ServiceWizard.constrain(1, 0, 1, 1, 1.0, 0.0, CENTER, HORIZONTAL);
        uiContainer.add(preferredCatalog, constraint);

        tip = messages.format("ServiceProfileOptions.preferred-schema.tip");
        title = messages.format("ServiceProfileOptions.preferred-schema.title");
        preferredSchema.setToolTipText(tip);
        label = new JLabel(title);
        label.setToolTipText(tip);
        label.setLabelFor(preferredSchema);
        constraint = ServiceWizard.constrain(0, 1, 1, 1, 0.0, 0.0, WEST, NONE);
        uiContainer.add(label, constraint);
        constraint = ServiceWizard.constrain(1, 1, 1, 1, 1.0, 0.0, CENTER, HORIZONTAL);
        uiContainer.add(preferredSchema, constraint);

        tip = messages.format("ServiceProfileOptions.secure-credentials.tip");
        title = messages.format("ServiceProfileOptions.secure-credentials.title");
        secureCredentials.setToolTipText(tip);
        label = new JLabel(title);
        label.setToolTipText(tip);
        label.setLabelFor(secureCredentials);
        constraint = ServiceWizard.constrain(0, 2, 1, 1, 0.0, 0.0, WEST, NONE);
        uiContainer.add(label, constraint);
        constraint = ServiceWizard.constrain(1, 2, 1, 1, 1.0, 0.0, CENTER, HORIZONTAL);
        uiContainer.add(secureCredentials, constraint);

        tip = messages.format("ServiceProfileOptions.service-debugging.tip");
        title = messages.format("ServiceProfileOptions.service-debugging.title");
        serviceDebugging.setToolTipText(tip);
        label = new JLabel(title);
        label.setToolTipText(tip);
        label.setLabelFor(serviceDebugging);
        constraint = ServiceWizard.constrain(0, 3, 1, 1, 0.0, 0.0, WEST, NONE);
        uiContainer.add(label, constraint);
        constraint = ServiceWizard.constrain(1, 3, 1, 1, 1.0, 0.0, CENTER, HORIZONTAL);
        uiContainer.add(serviceDebugging, constraint);

        tip = messages.format("ServiceProfileOptions.escape-processing.tip");
        title = messages.format("ServiceProfileOptions.escape-processing.title");
        escapeProcessing.setToolTipText(tip);
        label = new JLabel(title);
        label.setToolTipText(tip);
        label.setLabelFor(escapeProcessing);
        constraint = ServiceWizard.constrain(0, 4, 1, 1, 0.0, 0.0, WEST, NONE);
        uiContainer.add(label, constraint);
        constraint = ServiceWizard.constrain(1, 4, 1, 1, 1.0, 0.0, CENTER, HORIZONTAL);
        uiContainer.add(escapeProcessing, constraint);

        tip = messages.format("ServiceProfileOptions.generate-keys.tip");
        title = messages.format("ServiceProfileOptions.generate-keys.title");
        generateKeys.setToolTipText(tip);
        label = new JLabel(title);
        label.setToolTipText(tip);
        label.setLabelFor(generateKeys);
        constraint = ServiceWizard.constrain(0, 5, 1, 1, 0.0, 0.0, WEST, NONE);
        uiContainer.add(label, constraint);
        constraint = ServiceWizard.constrain(1, 5, 1, 1, 1.0, 0.0, CENTER, HORIZONTAL);
        uiContainer.add(generateKeys, constraint);

        constraint = ServiceWizard.constrain(0, 6, 2, 1, 1.0, 1.0, CENTER, VERTICAL);
        uiContainer.add(Box.createVerticalGlue(), constraint);
    }

    public boolean isValid(WizardContext context) {

        JdbcService service = (JdbcService) context.getAttribute(ServiceWizard.ATTRIBUTE_SERVICE);
        if (service == null) {
            return false;
        }
        ConnectionProfile profile = service.getProfile();
        profile.setPreferredCatalog(preferredCatalog.getText());
        profile.setPreferredSchema(preferredSchema.getText());
        profile.setCredentialsSecure(secureCredentials.isSelected());
        profile.setDebuggingEnabled(serviceDebugging.isSelected());
        profile.setEscapeProcessing(escapeProcessing.isSelected());
        profile.setResultsetKeys(generateKeys.isSelected());
        return true;
    }
}